Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
resolve-pkg
Advanced tools
The resolve-pkg npm package is used to resolve the path of a package regardless of where it is located in the node_modules hierarchy. This is particularly useful in monorepos or when dealing with nested dependencies.
Resolve Package Path
This feature allows you to resolve the absolute path of a package. The code sample demonstrates how to use resolve-pkg to find the path of 'some-package'.
const resolvePkg = require('resolve-pkg');
const packagePath = resolvePkg('some-package');
console.log(packagePath);
Resolve Package Path with CWD
This feature allows you to resolve the package path starting from a specific directory. The code sample shows how to specify a custom current working directory (cwd) for the search.
const resolvePkg = require('resolve-pkg');
const packagePath = resolvePkg('some-package', { cwd: '/path/to/start/search' });
console.log(packagePath);
The 'resolve' package is a dependency resolver that can find the full path to a module from a given starting point. It is more general-purpose compared to resolve-pkg, which is specifically designed for resolving package paths.
The 'pkg-dir' package finds the root directory of a Node.js project or a package. While resolve-pkg focuses on resolving the path to a specific package, pkg-dir is useful for finding the root directory of the current project.
The 'find-up' package searches for a file or directory by walking up parent directories. It is more versatile in terms of finding various types of files, whereas resolve-pkg is specialized in resolving package paths.
Resolve the path of a package regardless of it having an entry point
Some packages like CLI tools and grunt tasks don't have a entry point, like "main": "foo.js"
in package.json, resulting in them not being resolvable by require.resolve()
. Unlike require.resolve()
, this module also resolves packages without an entry point, returns undefined
instead of throwing when the module can't be found, and resolves from process.cwd()
instead __dirname
by default.
$ npm install resolve-pkg
const resolvePkg = require('resolve-pkg');
// $ npm install --save-dev grunt-svgmin
resolvePkg('grunt-svgmin/tasks', {cwd: __dirname});
//=> '/Users/sindresorhus/unicorn/node_modules/grunt-svgmin/tasks'
// Fails here as Grunt tasks usually don't have a defined main entry point
require.resolve('grunt-svgmin/tasks');
//=> Error: Cannot find module 'grunt-svgmin'
Type: string
What you would use in require()
.
Type: string
Default: process.cwd()
Directory to resolve from.
MIT © Sindre Sorhus
FAQs
Resolve the path of a package regardless of it having an entry point
We found that resolve-pkg demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.